package com.example.gymlearning.service.impl;

import com.example.gymlearning.dto.QuizQuestionDTO;
import com.example.gymlearning.entity.QuizQuestion;
import com.example.gymlearning.mapper.QuizQuestionMapper;
import com.example.gymlearning.service.QuestionRandomizationService;
import com.example.gymlearning.service.QuizQuestionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (QuizQuestion)表服务实现类
 *
 * @author makejava
 * @since 2025-11-03 13:44:18
 */
@Service("quizQuestionService")
@Slf4j  // 添加这行
public class QuizQuestionServiceImpl implements QuizQuestionService {
    @Resource
    private QuizQuestionMapper quizQuestionMapper;
    
    @Resource
    private QuestionRandomizationService questionRandomizationService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public QuizQuestion queryById(Long id) {
        return this.quizQuestionMapper.queryById(id);
    }

      /**
     * 新增数据
     *
     * @param quizQuestion 实例对象
     * @return 实例对象
     */
    @Override
    public QuizQuestion insert(QuizQuestion quizQuestion) {
        this.quizQuestionMapper.insert(quizQuestion);
        return quizQuestion;
    }

    /**
     * 修改数据
     *
     * @param quizQuestion 实例对象
     * @return 实例对象
     */
    @Override
    public QuizQuestion update(QuizQuestion quizQuestion) {
        this.quizQuestionMapper.update(quizQuestion);
        return this.queryById(quizQuestion.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.quizQuestionMapper.deleteById(id) > 0;
    }



    // QuizQuestionServiceImpl.java
// 添加以下实现：



    @Override
    public Long createPracticeSession(Long pointId) {
        // 创建练习会话的逻辑
        // 这里需要创建会话实体并保存
        return System.currentTimeMillis(); // 临时返回
    }

    @Override
    public Long createFormalSession(Long pointId) {
        // 创建正式会话的逻辑
        return System.currentTimeMillis(); // 临时返回
    }



    @Override
    public void completeSession(Long sessionId) {
        // 完成会话的逻辑
        log.info("完成会话: {}", sessionId);
    }

    @Override
    public boolean completeFormalSession(Long sessionId, Integer score, Integer correctAnswers, Integer totalQuestions) {
        // 完成正式会话并返回是否通过
        boolean isPassed = (correctAnswers * 100 / totalQuestions) >= 80; // 80%正确率通过
        log.info("完成正式闯关会话: {}, 得分: {}/{}, 通过: {}", sessionId, score, totalQuestions, isPassed);
        return isPassed;
    }

    @Override
    public List<QuizQuestion> getQuestionsByKnowledgePointId(Long pointId) {
        try {
            log.debug("开始查询题目，pointId: {}", pointId);
            List<QuizQuestion> questions = quizQuestionMapper.selectByKnowledgePointId(pointId);
            log.debug("查询到 {} 道题目", questions.size());
            return questions;
        } catch (Exception e) {
            log.error("根据知识点ID获取题目失败，pointId: {}", pointId, e);
            return Collections.emptyList();
        }
    }

    @Override
    public int getPracticeQuestionCount(Long pointId) {
        // 移除 usageType 过滤，返回所有题目
        return getQuestionsByKnowledgePointId(pointId).size();
    }

    @Override
    public int getFormalQuestionCount(Long pointId) {
        // 移除 usageType 过滤，返回所有题目
        return getQuestionsByKnowledgePointId(pointId).size();
    }

    @Override
    public List<QuizQuestion> getPracticeQuestions(Long pointId) {
        // 移除 usageType 过滤，返回所有题目
        return getQuestionsByKnowledgePointId(pointId);
    }

    @Override
    public List<QuizQuestion> getFormalQuestions(Long pointId) {
        // 移除 usageType 过滤，返回所有题目
        return getQuestionsByKnowledgePointId(pointId);
    }

    @Override
    public List<QuizQuestion> getRandomQuestionsByPointId(Long pointId, int count) {
        List<QuizQuestion> allQuestions = getQuestionsByKnowledgePointId(pointId);
        if (allQuestions.size() <= count) {
            return allQuestions;
        }

        // 随机选择题目
        Collections.shuffle(allQuestions);
        return allQuestions.subList(0, count);
    }
    
    @Override
    public List<QuizQuestionDTO> getRandomizedQuestions(Long pointId, String usageType) {
        return questionRandomizationService.getRandomizedQuestions(pointId, usageType);
    }
}